/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function getXmlHttpRequest()
{
    var xmlHttpObj;
    if (window.XMLHttpRequest)
    {
        xmlHttpObj = new XMLHttpRequest( );
    }
    else
    {
        try
        {
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                xmlHttpObj = false;
            }
        }
    }
    return xmlHttpObj;
}

var t;
function getFriend() {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url="userlist";
        self.xmlHttpReq.open('GET',url, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        document.getElementById("friend").innerHTML = self.xmlHttpReq.responseText;
        }
        }
        self.xmlHttpReq.send(null);
}
function getOnlineFriend() {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url="useronline";
        self.xmlHttpReq.open('GET',url, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        document.getElementById("friend").innerHTML = self.xmlHttpReq.responseText;
        }
        }
        self.xmlHttpReq.send(null);
}
function getRoom() {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url="roomServlet";
        self.xmlHttpReq.open('GET',url, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        document.getElementById("daftarroom").innerHTML = self.xmlHttpReq.responseText;
        }
        }
        self.xmlHttpReq.send(null);
}

function sendChat()
{
    xmlhttpchat = getXmlHttpRequest();
    if (!xmlhttpchat)
        return ;
    xmlhttpchat.open('POST', 'ChatProcessing', true);
    xmlhttpchat.onreadystatechange = printResponse;
    var chatcontent = "chatcontent=" + document.getElementById("chattextbox").value;
    //var curcategory = "currentcategory=" + document.getElementById("currentcategory").value;
    //var isroom = "isroom=" + document.getElementById("isroom").value;
    //var roomname = "roomname=" + document.getElementById("roomname").value;
    //var receiver = "receiver=" + document.getElementById("receiver").value;
    xmlhttpchat.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    //xmlhttpchat.send(chatcontent+ "&" +curcategory + "&" + isroom + "&" + roomname + "&" + receiver);
    xmlhttpchat.send(chatcontent);

    document.getElementById("chattextbox").value = "";
}
function printResponse()
{
    if (xmlhttpchat.readyState == 4 && xmlhttpchat.status == 200)
    {
        document.getElementById('ajaxInfo').innerHTML = xmlhttpchat.responseText;
    }
}
function chatFriend(str)
{
    chatFriend2(str);
    getchatFriend(str);
    //setTimeout("chatFriend(str)",1000);
}

function chatFriend2(str)
{
        var xmlHttpReq2 = false;
        var self2 = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
        self2.xmlHttpReq2 = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
        self2.xmlHttpReq2 = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url="chatfriend?friend="+str;
        self2.xmlHttpReq2.open('GET',url, true);
        self2.xmlHttpReq2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self2.xmlHttpReq2.onreadystatechange = function() {
        if (self2.xmlHttpReq2.readyState == 4) {
        document.getElementById("chattab").innerHTML = self2.xmlHttpReq2.responseText;
        }
        }
        self2.xmlHttpReq2.send(null);
}

function getchatFriend(str)
{
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url="getchatFriend?friend="+str;
        self.xmlHttpReq.open('GET',url, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        document.getElementById("chatbox").innerHTML = self.xmlHttpReq.responseText;
        }
        }
        self.xmlHttpReq.send(null);
        clearTimeout(t);
       t = setTimeout("getchatFriend('"+str+"')",1000);
}

function sendChatFriendjs2(str)
{
        xmlhttpchat = getXmlHttpRequest();
        if (!xmlhttpchat)
            return ;
        var arrchat = document.getElementById("textfriend").value;
        var X = arrchat.toString().substring(0, 1);
        var chatcontent = "chatcontent=" + document.getElementById("textfriend").value;
        if (X =="/")
        {
        xmlhttpchat.open('POST', 'ChatProcessing', true);
        xmlhttpchat.onreadystatechange = printResponseChat2;
        xmlhttpchat.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpchat.send(chatcontent);
        document.getElementById("textfriend").value="";
        }
        else
        {
        xmlhttpchat.open('POST', 'sendChatFriend', true);
        xmlhttpchat.onreadystatechange = printResponseChat;
        //var chatcontent = "chatcontent=" + document.getElementById("textfriend").value;
        var roomname = "roomname=" + document.getElementById("room").value;
        var rcv = "rcv=" + document.getElementById("receiver").value;
        //var receiver = str;
        var sender = "sender=" + document.getElementById("me").value;
        xmlhttpchat.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpchat.send(chatcontent+ "&" + rcv+ "&" + roomname +"&"+sender);
        document.getElementById("textfriend").value="";
        var stringku = document.getElementById("receiver").value;
        }

}
function sendChatFriendjs(str)
{
    getchatFriend(str);sendChatFriendjs2();
}
function printResponseChat()
{
    if (xmlhttpchat.readyState == 4 && xmlhttpchat.status == 200)
    {
        document.getElementById('chatbox').innerHTML = xmlhttpchat.responseText;
    }
}
function printResponseChat2()
{
    if (xmlhttpchat.readyState == 4 && xmlhttpchat.status == 200)
    {
        document.getElementById('responsechat').innerHTML = xmlhttpchat.responseText;
    }
}
function newtab(str)
{
    var a = document.getElementById("tab").innerHTML;
    document.getElementById("tab").innerHTML =  a + "<em><a href=\"javascript:activetabuser('" + str + "')\">" + str
        + "</a> <a href=\"javascript:closetab('" + str + "')\"> X </a>";    
}
function activetabuser(str)
{
    chatFriend(str);
}
function closetab(str)
{
    var a = document.getElementById("tab").innerHTML;
    var arrtab = a.toString().split("<em>");
    for (var i = 0; i < arrtab.length; i++)
    {
        var b = false;
        for (var j = 0; ((j < arrtab[i].length) && !b); j++)
        {
            if (arrtab[i].substr(j, str.length) == str)
            {
                arrtab[i] = "";
                b = true;
            }
        }
    }
    var res = "";
    for (var k = 0; k < arrtab.length; k++)
    {
        if (arrtab[k] != "")
        {
            res = res + "<em>" + arrtab[k];
        }
    }
    document.getElementById("tab").innerHTML = res;
    document.getElementById("chatbox").innerHTML ="";
    document.getElementById("chattab").innerHTML ="";}
function updatetab(str)
{
    var body = document.getElementById("tab");
    var update = document.createElement('div');
   update.id = 'str';
   body[0].appendChild(update);
}

